package com.wtp.wallpaper.es.Repository;

import com.wtp.wallpaper.es.document.PictureDocument;
import org.springframework.data.elasticsearch.annotations.Highlight;
import org.springframework.data.elasticsearch.annotations.HighlightField;
import org.springframework.data.elasticsearch.annotations.HighlightParameters;
import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface PictureDocumentRepository extends ElasticsearchRepository<PictureDocument, Long> {

    List<PictureDocument> findByTabsContaining(String keyword);

    //@Query("{\"multi_match\": {\"query\": \"?0\", \"fields\": [\"description\", \"tabs\"]}}")
    @Query("""
    {
      "multi_match": {
        "query": "?0",
        "fields": ["description", "tabs"],
        "type": "phrase"
      }
    }
    """)
    List<PictureDocument> searchByDescriptionAndTabs(String keyword);

    @Highlight(
            fields = @HighlightField(name = "description"),
            parameters = @HighlightParameters(
                    preTags = "<span style='color:#90EE90;font-style:normal'>",
                    postTags = "</span>",
                    fragmentSize = 200
            )
    )
    @Query("""
    {
      "multi_match": {
        "query": "?0",
        "fields": ["description", "tabs"],
        "type": "phrase"
      }
    }
    """)
    List<SearchHit<PictureDocument>> findByDescriptionContaining(String keyword);
}
